<template>
  <el-container class="home-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'">
      <div class="toggle-button" @click="toggleCollapse">|||</div>
      <!-- 侧边栏菜单区域 -->
      <el-menu
        background-color="#273543"
        text-color="#fff"
        active-text-color="#409eff"
        unique-opened
        :collapse="isCollapse"
        :collapse-transition="false"
        router
        :default-active="activePath"
      >
        <!-- 一级菜单 -->
        <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
          <!-- 一级菜单的模板区域 -->
          <template slot="title">
            <!-- 图标 -->
            <i :class="iconsObj[item.id]"></i>
            <!-- 文本 -->
            <span>{{ item.authName }}</span>
          </template>
          <!-- 二级菜单 -->
          <el-menu-item
            :index="'/' + subItem.path"
            v-for="subItem in item.children"
            :key="subItem.id"
            @click="savaNavState('/' + subItem.path)"
          >
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ subItem.authName }}</span>
            </template>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>微信小程序</div>
        <el-avatar :size="32" :src="avatarUrl"></el-avatar>
        <!-- <el-button type="info" @click="logout">退出</el-button> -->
        <el-menu mode="horizontal">
          <el-submenu index="1">
            <template slot="title">我的工作台</template>
            <el-menu-item index="1-1">订单信息</el-menu-item>
            <el-menu-item index="2-2">操作教程</el-menu-item>
            <el-menu-item index="2-3">清除缓存</el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">心选微淘</template>
            <el-menu-item index="2-1">设置</el-menu-item>
            <el-menu-item index="2-2" @click="logout">退出登录</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-header>
      <el-main>
		<transition>
			<!-- 路由占位符 -->
			<router-view></router-view>
		</transition>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单数据
      menuList: [],
      iconsObj: {
        "125": "el-icon-user",
        "103": "el-icon-lock",
        "101": "el-icon-goods",
        "102": "el-icon-document",
        "145": "el-icon-data-line"
      },
      // 是否折叠菜单
      isCollapse: false,
      // 被激活的链接地址
      activePath: "",
      // 头像路径
      avatarUrl: "https://qiniuyun.zhichi921.com/head_pic.jpg"
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$message.success("退出登录成功~");
      this.$router.push("/login");
    },
    // 获取所有分类列表
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      console.log(res);
    },
    // 点击按钮，实现切换菜单的展开与折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存链接的激活状态
    savaNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    }
  }
};
</script>

<style lang="scss" >
.home-container {
  height: 100%;
}

.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  height: 40px !important;
  & > div {
    color: #333;
    font-size: 14px;
  }
  .el-avatar {
    margin-left: auto;
    margin-right: 20px;
  }
}
.el-aside {
  background-color: #273543;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-submenu {
  i {
    color: #bfcbd9;
  }
}
.el-menu--horizontal > .el-submenu .el-submenu__title {
  height: 40px !important;
  line-height: 40px !important;
}
.el-menu--popup {
  min-width: 100px;
}
.v-enter{
	transform: translateX(50px);
	opacity: 0;
}
.v-enter-to{
	transform: translateX(0);
}
.v-enter-active{
	transition: .7s;
}
</style>
